<?php
/**
 * @author eagle
 * @version 2014-7-30 上午8:54:54
 * 每隔一个小时处理两个小时间的数据
 */
class Task_Ebaymailhour extends Minion_Task
{
    /**
     * Generates a help list for all tasks
     *
     * @return null
     */
    protected $_options = array(
        'start' => NULL,
        'end' => NULL,
    );

    //php f:\wamp\www\newerp\index.php --task=delivery --start="2014-03-27 " --end="2014-03-27 23:59:28"

    protected function _execute(array $params)
    {
        date_default_timezone_set('UTC');
        echo '任务[ebaymailhour]执行开始日期：' . date('Y-m-d H:i:s') . '\n';

        //防止进程过多
        system("ps aux|grep 'task=ebaymailhour'|wc -l", $retval);
        if (intval($retval) > 10) {
            echo '计划任务进程过多:' . $retval;
            exit;
        }

        $sql = "SELECT * FROM ebay_account
            WHERE ebay_token != ''
	        AND `lock`=0
	        LIMIT 5";
        $ebay_account_list = DB::query(Database::SELECT, $sql)
            ->execute('ebaymail')
            ->as_array();

        //将数据打乱
        shuffle($ebay_account_list);

        //开始时间的处理
        $start = date('Y-m-d H:i:s', time() - 60 * 120);
        foreach ($ebay_account_list as $key => $_account) {
            $end = date('Y-m-d H:i:s');
            ebayapi::get_member_messages($start, $end, $_account['ebay_token'], $_account['ebay_account']);
        }

        /* 检查错误的message */
        $sql = "SELECT * FROM errors_ackmessage WHERE status=0 LIMIT 0,1000 ";
        $errors_ackmessage_list = DB::query(Database::SELECT, $sql)
            ->execute('ebaymail')
            ->as_array();
        foreach ($errors_ackmessage_list as $_list) {
            $sql = "SELECT ebay_token FROM ebay_account WHERE ebay_account = '" . $ebay_account . "'";
            $ebay_account_temp = DB::query(Database::SELECT, $sql)
                ->execute('ebaymail')
                ->current();
            ebayapi::get_member_messages($_list['starttime'], $_list['endtime'], $ebay_account_temp['ebay_token'], $_list['ebay_account'], $_list['id']);
        }

        echo '任务[ebaymailhour]执行结束日期：' . date('Y-m-d H:i:s') . '\n';
    }
}
